#coding=utf-8

import urlparse

from django.shortcuts import render
from django.views.generic.base import View
from django.http import HttpResponse
import json
from test_case.models import TestProject,TestInterface,TestCace


#获取测试项目信息
class GetTestProject(View):
    def get(self,request):
        response = {
                    "code":"",
                    "data":{
                        "projectId":"",
                        "projectName":"",
                        "projectDesc":"",
                        "projectVersion":"",
                        "projectInterface":[]
                        }
                    }

        project_id = request.GET.get("project_id")
        try:
            project=TestProject.objects.get(project_id=project_id,is_activity=1)
        except Exception :
            return HttpResponse('{"code":1,"data":"null"}',content_type="application/json") 

        response["code"]=0
        response["data"]["projectId"]=project.project_id
        response["data"]["projectName"]=project.name
        response["data"]["projectDesc"]=project.desc
        response["data"]["projectVersion"]=project.version
        response["data"]["projectInterface"]=project.get_interface()

        return HttpResponse(json.dumps(response),content_type="application/json")


#获取测试接口信息
class GetTestInterface(View):
    def get(self,request):
        response = {
                    "code":"",
                    "data":{
                        "interfaceId":"",
                        "interfaceName":"",
                        "interfaceDesc":"",
                        "project":"",
                        "interfaceCase":[]
                        }
                    }

        interface_id = request.GET.get("interface_id")
        try:
            interface=TestInterface.objects.get(interface_id=interface_id,is_activity=1)
        except Exception :
            return HttpResponse('{"code":1,"data":"null"}',content_type="application/json") 

        response["code"]=0
        response["data"]["interfaceId"]=interface.interface_id
        response["data"]["interfaceName"]=interface.name
        response["data"]["interfaceDesc"]=interface.desc
        response["data"]["project"]=interface.project.name
        response["data"]["interfaceCase"]=interface.get_testcase()

        return HttpResponse(json.dumps(response),content_type="application/json")






#获取测试用例信息    
class GetTestCaseView(View):
    def get(self,request):
        response = {
                    "code":"",
                    "data":{
                        "caseId":"",
                        "caseName":"",
                        "caseDesc":"",
                        "project":"",
                        "interface":"",
                        "method":"",
                        "url":"",
                        "headers":"",
                        "requestBody":"",
                        "assertMethod":"",
                        }
                    }
        case_id = request.GET.get("case_id")
        try:
            case=TestCace.objects.get(case_id=case_id,is_activity=1)
        except Exception :
            return HttpResponse('{"code":1,"data":"null"}',content_type="application/json") 
        url="".join((case.interface.protocol,"://",case.interface.host,":",case.interface.port,case.interface.uri))
        response["code"]=0
        response["data"]["caseId"]=case.case_id
        response["data"]["caseName"]=case.name
        response["data"]["caseDesc"]=case.desc
        response["data"]["project"]=case.interface.project.name
        response["data"]["interface"]=case.interface.name
        response["data"]["method"]=case.interface.method
        response["data"]["url"]=url
        response["data"]["headers"]=case.interface.headers
        response["data"]["requestBody"]=case.request_body
        response["data"]["assertMethod"]=case.assert_method
        return HttpResponse(json.dumps(response),content_type="application/json")

